package com.demdevdudes.disch.gameobjects;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector3;
import com.demdevdudes.disch.commons.BaseAttributes;
import com.demdevdudes.disch.screen.utils.ScreenUtils;

public abstract class AbstractGameObject implements GameObject {

	private Vector3 coords;
	private ObjectState objectState = ObjectState.SOLID;
	private long millisToFade = System.currentTimeMillis();
	
	protected float alpha = 1.0f;
	
	public AbstractGameObject(float x, float y) {
		this(x, y, BaseAttributes.STD_ALPHA);
	}
	
	public AbstractGameObject(float x, float y, float alpha) {
		this.alpha = alpha;
		coords = new Vector3(x, y, BaseAttributes.STD_2D_Z);
	}
	
	public void update(float delta) {
		updateAlpha(delta);
	}
	
	public void fadeIn(long milliSecsToFade) {
		this.millisToFade = milliSecsToFade;
		objectState = ObjectState.FADE_IN;
	}

	public void fadeOut(long milliSecsToFade) {
		this.millisToFade = milliSecsToFade;
		objectState = ObjectState.FADE_OUT;
	}
	
	public void setCoords2d(float x, float y, OrthographicCamera camera) {
		coords.x = x;
		coords.y = y;
		coords.z = BaseAttributes.STD_2D_Z;
		coords = ScreenUtils.translateCoordinates(coords, camera);
	}
	
	public Vector3 getCoords2dVector() {
		return coords;
	}
	
	public ObjectState getObjectState() {
		return objectState;
	}
	
	public void updateAlpha(float delta) {
		long millisDelta = (long)(delta * 1000);
		float modAlpha = (float)millisDelta / (float)millisToFade;  
		switch(objectState) {
		case FADE_IN:
			if (alpha < 1.0f) {
				alpha += modAlpha;
				if (alpha >= 1.0f) {
					objectState = ObjectState.SOLID;
					alpha = 1.0f;
				}
			}
			break;
		case FADE_OUT:
			if (alpha > 0.0f) {
				alpha -= modAlpha;
				if (alpha <= 0.0f) {
					objectState = ObjectState.INVISIBLE;
					alpha = 0.0f;
				}
			}
			break;
		default:
			break;		
		}
	}
	
	public float getAlpha() {
		return alpha;
	} 
}
